package com.app.xinfadi.ui.purchase_plan.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.app.xinfadi.R;
import com.app.xinfadi.base.status.CenterRoleExaminApproveStatus;
import com.app.xinfadi.base.status.CenterRoleNewAddStatus;
import com.app.xinfadi.base.status.LeanRoleStatus;
import com.app.xinfadi.dao_bean.bean.CenterPurchasePlanBean;
import com.app.xinfadi.base.status.PlanStatusEnum;
import com.app.xinfadi.util.SharedPreferencesUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.ArrayList;
import java.util.List;

public class CenterPurchasePlanListAdapter extends BaseAdapter {
    private Context context;
    private List<CenterPurchasePlanBean> data;
    public DisplayImageOptions options;
    public final String TAG = CenterPurchasePlanListAdapter.class.getCanonicalName();
    /**
     * 0 审核列表
     * 1 中心新增列表
     */
    private String center_status;

    public CenterPurchasePlanListAdapter(Context context) {
        this.context = context;
        data = new ArrayList<CenterPurchasePlanBean>();
    }

    public CenterPurchasePlanListAdapter(Context context, String center_status) {
        this.context = context;
        data = new ArrayList<CenterPurchasePlanBean>();
        this.center_status = center_status;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        // 正常列表
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.center_purchase_plan_list_item_layout, null);
            holder = new ViewHolder();
            holder.purchaseIdTv = (TextView) convertView.findViewById(R.id.purchaseIdTv);
            holder.purchaseNameTv = (TextView) convertView.findViewById(R.id.purchaseNameTv);
            holder.purchaseNumberTv = (TextView) convertView.findViewById(R.id.purchaseNumberTv);
            holder.purchaseUnit = (TextView) convertView.findViewById(R.id.purchaseUnit);
            holder.expiryDateTv = (TextView) convertView.findViewById(R.id.expiryDateTv);
            holder.purchaseDeliveryTimeTv = (TextView) convertView.findViewById(R.id.purchaseDeliveryTimeTv);
            holder.buyerTv = (TextView) convertView.findViewById(R.id.buyerTv);
            holder.statusTv = (TextView) convertView.findViewById(R.id.statusTv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (!TextUtils.isEmpty(data.get(position).NAME)) {
            holder.purchaseIdTv.setText(data.get(position).NAME);
        }
        if (!TextUtils.isEmpty(data.get(position).PRODUCTS_NAME)) {
            holder.purchaseNameTv.setText(data.get(position).PRODUCTS_NAME);
        }
        if (!TextUtils.isEmpty(data.get(position).PURCHASE_COUNT)) {
            holder.purchaseNumberTv.setText(data.get(position).PURCHASE_COUNT);
        }
        if (!TextUtils.isEmpty(data.get(position).STANDARD)) {
            holder.purchaseUnit.setText(data.get(position).STANDARD);
        }
        if (!TextUtils.isEmpty(data.get(position).EXPIRY_DATE)) {
            holder.expiryDateTv.setText(data.get(position).EXPIRY_DATE);
        }
        if (!TextUtils.isEmpty(data.get(position).DELIVERY_TIME)) {
            holder.purchaseDeliveryTimeTv.setText(data.get(position).DELIVERY_TIME);
        }
        if (!TextUtils.isEmpty(data.get(position).MAKER)) {
            holder.buyerTv.setText(data.get(position).MAKER);
        }
        if (!TextUtils.isEmpty(data.get(position).STATUS)) {
            holder.statusTv.setText(getStatusName(data.get(position).STATUS));
//            LogUtil.e(TAG,data.get(position).STATUS+"  "+getStatusName(data.get(position).STATUS));
        }
        return convertView;
    }


    public String getStatusName(String statusCode) {
        String type = SharedPreferencesUtil.getInstance().getUserRole();

        if (type.equals("0")) {

            return PlanStatusEnum.getInstance().getStatusName(statusCode);
//            for (PlanStatusEnum c : PlanStatusEnum.values()) {
//                if (c.getStatusCode().equals(statusCode)) {
//                    return c.getName();
//                }
//            }
        } else if (type.equals("1")) {
            if (center_status.equals("0")) {
                return CenterRoleExaminApproveStatus.getInstance().getStatusName(statusCode);
//                for (CenterRoleExaminApproveStatus c : CenterRoleExaminApproveStatus.values()) {
//                    if (c.getStatusCode().equals(statusCode)) {
//                        return c.getName();
//                    }
//                }
            } else if (center_status.equals("1")) {
                return CenterRoleNewAddStatus.getInstance().getStatusName(statusCode);

//                for (CenterRoleNewAddStatus c : CenterRoleNewAddStatus.values()) {
//                    if (c.getStatusCode().equals(statusCode)) {
//                        return c.getName();
//                    }
//                }
            }
        } else if (type.equals("2")) {
            return LeanRoleStatus.getInstance().getStatusName(statusCode);

//            for (LeanRoleStatus c : LeanRoleStatus.values()) {
//                if (c.getStatusCode().equals(statusCode)) {
//                    return c.getName();
//                }
//            }
        }

        return "";
    }

    class ViewHolder {
        TextView purchaseIdTv;
        TextView purchaseNameTv;
        TextView purchaseNumberTv;
        TextView purchaseUnit;
        TextView expiryDateTv;
        TextView purchaseDeliveryTimeTv;
        TextView buyerTv;
        TextView statusTv;
    }

    public List<CenterPurchasePlanBean> getData() {
        return data;
    }

    public void setData(List<CenterPurchasePlanBean> data) {
        this.data = data;
    }

}
